﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlServerCe;
using System.Data;
using System.ComponentModel;
using MyTime.Data;
using System.IO;

namespace MyTime
{
    /// <summary>
    /// Interaction logic for TimeViewer.xaml
    /// </summary>
    public partial class TimeViewer : Window
    {
        private string connectionString = @"Data Source=.\Data\MyTimeData.sdf; password='mySecureDat'";

        private Point startPoint;

        private List<TaskLog> taskData;
        public List<TaskLog> TaskData
        {
            get { return taskData; }
            set
            {
                taskData = value;
                OnPropertyChanged("TaskData");
            }
        }

        public TimeViewer()
        {
            TaskData = new List<TaskLog>();
            LoadData();      
            InitializeComponent();
                  
            
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void LoadData()
        {
            MyTimeData data = new MyTimeData(connectionString);
            TaskData = data.TaskLog.OrderByDescending(x => x.StartTime).ToList();
           
            //SqlCeConnection _Conn = new SqlCeConnection(connectionString);

            //// Open the Database Connection
            //_Conn.Open();

            //// Command String
            //string _Insert = @"SELECT TaskLog.* FROM TaskLog oder by TimeStamp desc";

            //// Initialize the command query and connection
            //SqlCeCommand _cmd = new SqlCeCommand(_Insert, _Conn);

            //// Execute the command
            //SqlCeDataAdapter adapter = new SqlCeDataAdapter(_cmd);
            //adapter.Fill(TaskData);            
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            LoadData();
        }

        private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Store the mouse position
            startPoint = e.GetPosition(null);
        }

        private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            // Get the current mouse position
            Point mousePos = e.GetPosition(null);
            Vector diff = startPoint - mousePos;

            if (e.LeftButton == MouseButtonState.Pressed &&
               ( Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
                Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
            {
                // Get the dragged ListViewItem
                Label listView = sender as Label;
                //DataGridRow listViewItem = FindAnchestor<DataGridRow>((DependencyObject)e.OriginalSource);

                if (listView != null && listView.Content!=null && !string.IsNullOrEmpty(listView.Content.ToString()))
                {
                    // Find the data behind the ListViewItem
                    
                    var te = listView.Content.ToString();

                    // Initialize the drag & drop operation
                    string[] paths = new string[] { te };

                    //StreamReader str = new StreamReader("New Text Document.txt");
                    DataObject dragData = new DataObject(DataFormats.FileDrop, paths);
                    DragDrop.DoDragDrop(dataGrid1, dragData, DragDropEffects.Copy);
                }
            }
        }

        // Helper to search up the VisualTree
        private static T FindAnchestor<T>(DependencyObject current)
            where T : DependencyObject
        {
            do
            {
                if (current is T)
                {
                    return (T)current;
                }
                current = VisualTreeHelper.GetParent(current);
            }
            while (current != null);
            return null;
        }

    }
}
